﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace HuiJI.Tools.TBK
{
    public class TBKTools
    {
        private string _url { get; set; }

        public TBKTools(string url)
        {

            this._url = url;
        }
        /// <summary>
        /// 淘抢购api 获取淘抢购的数据，淘客商品转淘客链接，非淘客商品输出普通链接
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="secret"></param>
        /// <param name="adzoneId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public TbkJuTqgGetResponse TaoQiangGou(string appkey, string secret, long adzoneId, DateTime startTime, DateTime endTime, long page = 1, long pageSize = 40)
        {
            ITopClient client = new DefaultTopClient(_url, appkey, secret);
            TbkJuTqgGetRequest req = new TbkJuTqgGetRequest();
            req.AdzoneId = adzoneId;
            req.Fields = "click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time";
            req.StartTime = startTime;
            req.EndTime = endTime;
            req.PageNo = page;
            req.PageSize = pageSize;
            TbkJuTqgGetResponse rsp = client.Execute(req);
            return rsp;
        }
        /// <summary>
        /// 物料精选
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="secret"></param>
        /// <param name="adzoneId">mm_xxx_xxx_xxx的第三位</param>
        /// <param name="materialId">官方的物料Id(详细物料id见：https://market.m.taobao.com/app/qn/toutiao-new/index-pc.html#/detail/10628875?_k=gpov9a)</param>
        /// <param name="pageSize">页大小，默认20，1~100</param>
        /// <param name="pageNo">第几页</param>
        /// <param name="itemId">商品ID，用于相似商品推荐</param>
        /// <returns></returns>
        public TbkDgOptimusMaterialResponse Material(string appkey, string secret, long adzoneId, long materialId, int pageSize, int pageNo, long? itemId)
        {

            ITopClient client = new DefaultTopClient(_url, appkey, secret);
            TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
            req.PageSize = pageSize;
            req.AdzoneId = adzoneId;
            req.PageNo = pageNo;
            req.MaterialId = materialId;
            req.DeviceValue = null;
            req.DeviceEncrypt = null;
            req.DeviceType = null;
            req.ContentId = null;
            req.ContentSource = null;
            req.ItemId = itemId;
            req.FavoritesId = null;
            TbkDgOptimusMaterialResponse rsp = client.Execute(req);
            return rsp;

        }

        /// <summary>
        /// 官方活动转链-官方活动信息获取
        /// </summary>
        /// <param name="activity_material_id">官方活动物料id</param>
        public TbkActivityInfoGetResponse ActivityInfoGet(string appkey, string secret, long adzoneId, string activity_material_id)
        {

            ITopClient client = new DefaultTopClient(_url, appkey, secret);
            TbkActivityInfoGetRequest req = new TbkActivityInfoGetRequest();
            req.AdzoneId = adzoneId;
            //req.SubPid = "mm_1_2_3"; //mm_xxx_xxx_xxx 仅三方分成场景使用
            //req.RelationId = 123L;//代理id
            req.ActivityMaterialId = activity_material_id;//官方活动物料id，由官方运营提供或从官方平台上获取
            //req.UnionId = "demo"; //自定义输入串，英文和数字组成，长度不能大于12个字符，区分不同的推广渠道
            TbkActivityInfoGetResponse rsp = client.Execute(req);
            return rsp;
        }

        /// <summary>
        /// 淘口令获取
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="secret"></param>
        /// <param name="url"></param>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <param name="logo"></param>
        /// <returns></returns>
        public TbkTpwdCreateResponse TKL(string appkey, string secret, string url, string userId, string title, string logo = "https://uland.taobao.com/")
        {
            if (!url.Contains("https:"))
            {
                url = "https:" + url;
            }
            ITopClient client = new DefaultTopClient(_url, appkey, secret);
            TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
            req.UserId = userId;
            req.Logo = logo;
            req.Text = title;
            req.Url = url;
            TbkTpwdCreateResponse response = client.Execute(req);
            return response;
        }

        /// <summary>
        /// 物料搜索
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="secret"></param>
        /// <param name="adzoneId"></param>
        /// <param name="page">页码</param>
        /// <param name="pageSize">页大小 最大100</param>
        /// <param name="key">关键字</param>
        /// <param name="sort">排序</param>
        /// <param name="startPrice">价格区间</param>
        /// <param name="endPrice">价格区间</param>
        /// <returns></returns>
        public TbkDgMaterialOptionalResponse MaterialOptional(string appkey, string secret, long adzoneId, int page,int pageSize,string key,string sort,long? startPrice,long? endPrice)
        {
            ITopClient client = new DefaultTopClient(_url, appkey, secret);
            TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
            req.StartDsr = 0L;//商品筛选(特定媒体支持)-店铺dsr评分。筛选大于等于当前设置的店铺dsr评分的商品0-50000之间
            req.PageSize = pageSize;//页大小，默认20，1~100
            req.PageNo = page;//第几页，默认：１
            req.Platform = 2L;//链接形式：1：PC，2：无线，默认：１
            //req.EndTkRate = 1234L;//商品筛选-淘客佣金比率上限。如：1234表示12.34%
            //req.StartTkRate = 1234L;//商品筛选-淘客佣金比率下限。如：1234表示12.34%
            req.EndPrice = endPrice;//商品筛选-折扣价范围上限。单位：元
            req.StartPrice = startPrice;//商品筛选-折扣价范围下限。单位：元
            req.IsOverseas = false;//商品筛选-是否海外商品。true表示属于海外商品，false或不设置表示不限
            req.IsTmall = false;//商品筛选-是否天猫商品。true表示属于天猫商品，false或不设置表示不限
            req.Sort = sort;//排序_des（降序），排序_asc（升序），销量（total_sales），淘客佣金比率（tk_rate）， 累计推广量（tk_total_sales），总支出佣金（tk_total_commi），价格（price）
            //req.Itemloc = "杭州";//商品筛选-所在地
            //req.Cat = "16,18";//商品筛选-后台类目ID。用,分割，最大10个，该ID可以通过taobao.itemcats.get接口获取到
            req.Q = key;//商品筛选-查询词
            req.MaterialId = 2836L;//不传时默认物料id=2836；如果直接对消费者投放，可使用官方个性化算法优化的搜索物料id=17004
            req.HasCoupon = false;//优惠券筛选-是否有优惠券。true表示该商品有优惠券，false或不设置表示不限
            //req.Ip = "13.2.33.4";//ip参数影响邮费获取，如果不传或者传入不准确，邮费无法精准提供
            req.AdzoneId = adzoneId;//mm_xxx_xxx_12345678三段式的最后一段数字
            req.NeedFreeShipment = true;//商品筛选-是否包邮。true表示包邮，false或不设置表示不限
            req.NeedPrepay = true;//商品筛选-是否加入消费者保障。true表示加入，false或不设置表示不限
            //req.IncludePayRate30 = true;//商品筛选(特定媒体支持)-成交转化是否高于行业均值。True表示大于等于，false或不设置表示不限
            //req.IncludeGoodRate = true;//商品筛选-好评率是否高于行业均值。True表示大于等于，false或不设置表示不限
            //req.IncludeRfdRate = true;//商品筛选(特定媒体支持)-退款率是否低于行业均值。True表示大于等于，false或不设置表示不限
            //req.NpxLevel = 2L;//商品筛选-牛皮癣程度。取值：1不限，2无，3轻微
            //req.EndKaTkRate = 1234L;//商品筛选-KA媒体淘客佣金比率上限。如：1234表示12.34%
            //req.StartKaTkRate = 1234L;//商品筛选-KA媒体淘客佣金比率下限。如：1234表示12.34%
            //req.DeviceEncrypt = "MD5";//智能匹配-设备号加密类型：MD5
            //req.DeviceValue = "xxx";//智能匹配-设备号加密后的值（MD5加密需32位小写）
            //req.DeviceType = "IMEI";//智能匹配-设备号类型：IMEI，或者IDFA，或者UTDID（UTDID不支持MD5加密），或者OAID
            //req.LockRateEndTime = 1567440000000L;//锁佣结束时间
            //req.LockRateStartTime = 1567440000000L;//锁佣开始时间
            //req.Longitude = "121.473701";//本地化业务入参-LBS信息-经度
            //req.Latitude = "31.230370";//本地化业务入参-LBS信息-纬度
            //req.CityCode = "310000";//本地化业务入参-LBS信息-国标城市码，仅支持单个请求，请求饿了么卡券物料时，该字段必填。 （详细城市ID见：https://mo.m.taobao.com/page_2020010315120200508）
            //req.SellerIds = "1,2,3,4";//商家id，仅支持饿了么卡券商家ID，支持批量请求1-100以内，多个商家ID使用英文逗号分隔
            //req.SpecialId = "2323";//会员运营ID
            //req.RelationId = "3243";//渠道关系ID，仅适用于渠道推广场景
            TbkDgMaterialOptionalResponse rsp = client.Execute(req);
            return rsp;
        }
    }
}
